package de.hpi.generator.helpers;

import de.hpi.generationmanager.tesma.data.TesmaDataTable;
import java.io.IOException;


import de.hpi.generator.model.Column;
import de.hpi.generator.model.Database;
import de.hpi.generator.model.Table;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class FileJoiner {

    protected static String aCsvSeparator = ",";
    private static final int WRITE_BUFFER_SIZE_IN_BYTES = 1000000;

    public static void compileTableData(Table aTable) throws IOException, Exception {
        BufferedWriter aFileWriterForTable = getFileWriterForTable(aTable);
        aTable.openForRead();
        
        aFileWriterForTable.write(createHeader(aTable));
        
        int aNumberOfRowsInTable = getNumberOfRowsInTable(aTable);
        for (int aRowNumber = 0; aRowNumber < aNumberOfRowsInTable; aRowNumber++) {
            aFileWriterForTable.write(readRowAsStringFromTable(aTable));
        }
        
        aTable.closeAfterRead();
        aFileWriterForTable.close();
    }

    private static BufferedWriter getFileWriterForTable(Table aTable) throws Exception {
        String aOutputFileNameForTable = getOutputFileNameForTableForTable(aTable);
        FileHelper.recreateFile(aOutputFileNameForTable);
        return new BufferedWriter(new FileWriter(aOutputFileNameForTable), WRITE_BUFFER_SIZE_IN_BYTES);
    }

    private static int getNumberOfRowsInTable(Table aTable) {
        int aNumberOfValues = aTable.getColumns().get(0).getNumberOfValues();
        return aNumberOfValues;
    }

    public static String getOutputFileNameForTableForTable(Table aTable) {
        String aOutputDirectory = aTable.getOwner().getWorkingDirectoryPath() + "\\output";
        String aOutputFileNameForTable = aOutputDirectory + "\\" + aTable.getName() + ".csv";
        return aOutputFileNameForTable;
    }

    protected static String readRowAsStringFromTable(Table aTable) throws Exception {
        StringBuilder aResultStringBuilder = new StringBuilder();
        for (Column aColumn : aTable.getColumns()) {
            if (aResultStringBuilder.length() > 0) {
                aResultStringBuilder.append(aCsvSeparator);
            }
            int aValue = aColumn.readNextValueSeriaInfo().getValue();
            String aStringValue = aColumn.convertToString(aValue);
            aResultStringBuilder.append(aStringValue);

        }

        aResultStringBuilder.append("\n");
        return aResultStringBuilder.toString();
    }

    public static void compileCSVFromGeneratedData(Database aDatabase) throws IOException, Exception {
        for (Table aTable : aDatabase.getTables()) {
            if (aTable.getName().endsWith(TesmaDataTable.NAME_SUFFIX)) {
                System.out.println("Skip tesma table" + aTable.getName());
                continue;
            }
            System.out.println("Creating an output file for " + aTable.getName());
            compileTableData(aTable);
        }
    }

    private static String createHeader(Table aTable) {
        StringBuilder result = new StringBuilder();
        for (Column aColumn : aTable.getColumns()) {
            if (result.length() > 0) {
                result.append(aCsvSeparator);
            }

            result.append(aColumn.getName());
        }
        result.append("\n");
        return result.toString();
    }
}
